import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Vector;


public class Quiz
{
	public Quiz(String filename)
	{
		this.setFilename(filename);
	}

	public Quiz(String quizname, String filename)
	{
		this.quizname = quizname;
		this.setFilename(filename);
	}
	
	public void read() throws IOException
	{
		 FileInputStream fstream = new FileInputStream(getFilename());
		 DataInputStream in = new DataInputStream(fstream);
		 BufferedReader br = new BufferedReader(new InputStreamReader(in));
		 
		 quizname = br.readLine();
		 Question q;
		 while((q = readQuestion(br)) != null)
		 {
			 itsQuestion.add(q);
		 }
	}
	
	private Question readQuestion(BufferedReader br) throws IOException
	{
		String title, text, imageFile, sel1, sel2, sel3, sel4, answer;
		title = text = imageFile = sel1 = sel2 = sel3 = sel4 = null;
		
		String header = br.readLine();
		if(header == null || !header.equals("[Q]"))
		{
			return null;
		}
		imageFile = br.readLine();
		title = br.readLine();
		text = br.readLine();
		sel1 = br.readLine();
		sel2 = br.readLine();;
		sel3 = br.readLine();
		sel4 = br.readLine();
		answer = br.readLine();
		
		if(title == null || text == null || imageFile == null || 
			sel1 == null || sel2 == null || sel3 == null || sel4 == null)
		{
			return null;
		}
		else
		{
			Question q = new Question(title, text, imageFile);
			q.setSelection(0, sel1);
			q.setSelection(1, sel2);
			q.setSelection(2, sel3);
			q.setSelection(3, sel4);
			q.setAnswer(Integer.parseInt(answer));
			return q;
		}
	}
	
	public void write() throws IOException
	{
		// create if doesn't exist, if it does clear the contents
		File old = new File(getFilename());
		old.createNewFile();
		old.delete();
		File file = new File(getFilename());
		file.createNewFile();
		
		
		FileWriter fWriter = new FileWriter(getFilename());
		PrintWriter out = new PrintWriter(fWriter);
		out.println(quizname);
		for(int i = 0; i < itsQuestion.size(); ++i)
		{
			Question q = getQuestion(i);
			out.println("[Q]");
			out.println(q.getImageFile());
			out.println(q.getTitle());
			out.println(q.getText());
			out.println(q.getSelection(0));
			out.println(q.getSelection(1));
			out.println(q.getSelection(2));
			out.println(q.getSelection(3));
			out.println(Integer.toString(q.getAnswer()));
		}
		
		out.close();
	}
	
	public int size()
	{
		return itsQuestion.size();
	}
	
	public Question getQuestion(int index)
	{
		if(index > -1 && index < itsQuestion.size())
		{
			return itsQuestion.get(index);
		}
		else
		{
			return null;
		}
	}
	
	public void addQuestion(int index, Question q)
	{
		itsQuestion.add(index, q);
	}
	
	public void addQuestion(Question q)
	{
		itsQuestion.add(q);
	}
	
	public void removeQuestion(int index)
	{
		if(index > -1 && index < itsQuestion.size())
		{
			itsQuestion.remove(index);
		}
	}
	
	
	public String getFilename()
	{
		return filename;
	}
	

	public void setFilename(String filename)
	{
		this.filename = filename;
	}
	
	public String getQuizname()
	{
		return quizname;
	}



	private Vector<Question> itsQuestion = new Vector<Question>();
	private String filename, quizname;
}
